<?php
//*****************************************************************//
// Pixie: The Small, Simple, Site Maker.                           //
// ----------------------------------------------------------------//
// Licence: GNU General Public License v3                   	   //
// Title: Your Module Name	                                       //
//*****************************************************************//

//----------------------------------------------------------

//******DATABASE FUNCTIONS******//

//----------------------------------------------------------

//Get the IDs of all galleries

function getGalleryListOfIDs()
{
	//generate and execute query
	$listQuery = "SELECT galleries_id FROM pixie_module_galleries";
	$listResult = mysql_query($listQuery) or die("Error in query: $query..." . mysql_error());
	
	while ($listRow = mysql_fetch_array($listResult, MYSQL_NUM))
	{
		$galleryListOfIDs[] = $listRow[0];
	}
		
	return $galleryListOfIDs;
}

//----------------------------------------------------------

//Get the gallery info for display in the list

function getGalleryInfo($galleryID)
{
	//generate and execute query
	$galleryQuery = "SELECT title, summary, description, date, sourcefolder FROM pixie_module_galleries WHERE galleries_id = '$galleryID'";
	$galleryResult = mysql_query($galleryQuery) or die("Error in query: $query..." . mysql_error());
	
	$galleryInfo = array();
	
	$galleryRow = mysql_fetch_array($galleryResult, MYSQL_NUM);
	
	for ($i = 0; $i < sizeof($galleryRow); $i++)
	{
		$galleryInfo[$i] = $galleryRow[$i];
	}
	
	return $galleryInfo;
}

//----------------------------------------------------------

//Get the gallery sourcefolder

function getSourceFolder($galleryID)
{
	//generate and execute query
	$galleryQuery = "SELECT sourcefolder FROM pixie_module_galleries WHERE galleries_id = '$galleryID'";
	$galleryResult = mysql_query($galleryQuery) or die("Error in query: $query..." . mysql_error());
	
	$galleryRow = mysql_fetch_array($galleryResult, MYSQL_NUM);
	
	for ($i = 0; $i < sizeof($galleryRow); $i++)
	{
		$gallerySourceFolder = $galleryRow[$i];
	}
	
	return $gallerySourceFolder;
}


//----------------------------------------------------------

//******STRING FORMAT FUNCTIONS******//

//Return the date in a nice format
function formatDate($longDate)
{
	$arr = explode('-', $longDate);
	return date('dS F Y', mktime(0,0,0, $arr[1], $arr[2], $arr[0]));
}

//----------------------------------------------------------

// **** Return true is the filename is a JPEG file

//----------------------------------------------------------
function isImage($fileName)
  {
    //check if filename is valid
    if ($fileName[0] == "." || $fileName[0] == ".." ) 
    {
      return false;
    }
    
    $components = explode(".", $fileName);
    $length = count($components);
    
    
    if ($length == 1) 
    {
      return false;
    }
    
    $suffix = strtolower($components[$length - 1]);
    
    if ($suffix == 'jpg' || $suffix == 'jpeg') 
    {
      return true;
    }
    return false;
  }

//----------------------------------------------------------

// **** create thumbnails for the images 

//----------------------------------------------------------  
  
function createThumbs( $pathToImages, $pathToThumbs, $thumbWidth ) 
{
  //add forward slash at the end of the path name
  $pathToImages = $pathToImages."/";
  $pathToThumbs = $pathToThumbs."/";
  
  // open the directory
  $imageDir = opendir( $pathToImages );

  // loop through it, looking for any/all JPG files:
  while (false !== ($fname = readdir( $imageDir ))) {

    // continue only if this is a JPEG image
    if (isImage($fname)) 
    {      
      // load image and get image size
      $img = imagecreatefromjpeg( "{$pathToImages}{$fname}" );
      $width = imagesx( $img );
      $height = imagesy( $img );
      
      // calculate thumbnail size
      $new_width = $thumbWidth;
      
      //check if picture is vertical or horizontal
      if ($width > $height)   //horizontal
      { 
        $new_height = floor( $height  * ( $thumbWidth / $width ) );
      }
      else      //vertical
      {
        $new_height = floor( ($height/1.3)  * ( $thumbWidth / $width ) );
      }

      // create a new temporary image
      $tmp_img = imagecreatetruecolor( $new_width, $new_height );

      // copy and resize old image into new image 
      imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );

      
      // save thumbnail into a file
      imagejpeg($tmp_img, "{$pathToThumbs}{$fname}", 100);
      
      //destroy temporary image
      imagedestroy($tmp_img);
      
    }
  }
  // close the directory
  closedir( $imageDir );
  return true;
}

?>